/*
 * ProjectView.java
 * 
 * last update: 14.01.2010 by Stefan Saru
 * 
 * author:	Felix(felix@ulise.cs.pub.ro)
 * 			Alec (panovici@elcom.pub.ro)
 *
 * Obs: N/A
 */

package ide;


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.tree.*;
import java.io.File;

@SuppressWarnings("serial")
public class ProjectView extends JScrollPane {  

	public JTree projectTree;
	Project project;

	public ProjectView(Project project) {


		super(); 
		vide.spl.showStatus("Creating project browser..");

		this.project = project;
		project.pv = this;
		//TODO : instead of the null, we may add some per project info file to which we could jump
		projectTree = new JTree(new DefaultMutableTreeNode(new ProjectTreeNode(null, project.name,
				-1, ProjectTreeNode.PROJECTTYPE)));

		projectTree.setShowsRootHandles(true);
		projectTree.setCellRenderer(new ProjectTreeCellRenderer());
		//JScrollPane treeScroller = new JScrollPane(projectTree);
		getViewport().add(projectTree);
		//treeScroller.
		setBackground(Color.white);//projectTree.getBackground());

		MouseListener ml = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {                                      
				int selRow = projectTree.getRowForLocation(e.getX(), e.getY());
				if((selRow != -1) && (e.getClickCount() == 1)) { //one clock is far enough !
					itemDClicked(projectTree.getPathForLocation(e.getX(), e.getY()));
				}
			}
		};

		projectTree.addMouseListener(ml);
		vide.spl.showStatus("Creating project browser..done");

	} 

	public void clear() {
		DefaultTreeModel model = (DefaultTreeModel) projectTree.getModel();

		DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
		synchronized(root) {
			root.removeAllChildren();
		}
		model.nodeStructureChanged(root);
	}

	public void addFile(String fileName) {
		//todo
		removeFile(fileName);
		fileName = new java.io.File(fileName).getAbsolutePath();  //convert to
		//absolute path
		ProjectViewFactory pv = new ProjectViewFactory(project, fileName,
				(DefaultTreeModel)
				projectTree.getModel());

		/*
    ((DefaultTreeModel) projectTree.getModel()).nodeStructureChanged((DefaultMutableTreeNode)
                                                                     ((DefaultTreeModel) projectTree.getModel()).getRoot());
		 */
		try {
			pv.updateProjectTree();
		} catch(Exception ex) {
			project.error(ex.getMessage());
		}
		((DefaultTreeModel) projectTree.getModel()).nodeStructureChanged((DefaultMutableTreeNode)
				((DefaultTreeModel) projectTree.getModel()).getRoot());
		projectTree.invalidate();
	}

	public void removeFile(String fileName) {    //musai cale absoluta?
		//todo: nu merge.
		System.out.println(fileName);
		fileName = new java.io.File(fileName).getAbsolutePath();
		DefaultTreeModel treeModel = (DefaultTreeModel) projectTree.getModel();
		DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeModel.getRoot();

		//    ProjectTreeNode nodeInfo = new ProjectTreeNode(fileName, 1, ProjectTreeNode.FILETYPE);

		int index = 0;
		for(Enumeration e = root.children() ; e.hasMoreElements() ; index++){
			DefaultMutableTreeNode tn = (DefaultMutableTreeNode)e.nextElement();
			System.out.println(((ProjectTreeNode)tn.getUserObject()).fileName + "<-->"
					+ fileName);
			if(((ProjectTreeNode)tn.getUserObject()).fileName.equals(fileName)){  //Alec-> since a fileName field
				//was added to ProjTreeNode, 
				//this should work
				root.remove(index);
				break;
			}
		}

		((DefaultTreeModel) projectTree.getModel()).
		nodeStructureChanged((DefaultMutableTreeNode) ((DefaultTreeModel)
				projectTree.
				getModel()).getRoot());
	}

	public void itemDClicked(TreePath path) {
		Object obj = path.getLastPathComponent();
		ProjectTreeNode node = (ProjectTreeNode) ((DefaultMutableTreeNode) obj).getUserObject();
		switch(node.type) {
		case ProjectTreeNode.PROJECTTYPE : break;  //do nothing
		case ProjectTreeNode.FILETYPE :
		case ProjectTreeNode.INCLUDETYPE:
			project.jumpToLocation(node.fileName, 0);  // at the begining of file
			break;
			/* //si asta intra la cazul general
    case ProjectTreeNode.MODULETYPE : 
      ProjectTreeNode prevNode = (ProjectTreeNode) ((DefaultMutableTreeNode)
                                                    path.getPathComponent(1)).getUserObject();
      project.jumpToLocation(prevNode.name, node.line);
      break;
			 */
			/*
    case ProjectTreeNode.TASKTYPE :
    case ProjectTreeNode.FUNCTIONTYPE :
    case ProjectTreeNode.BLOCKTYPE :
    case ProjectTreeNode.PORTTYPE : 
    case ProjectTreeNode.REGTYPE : 
    case ProjectTreeNode.INOUTTYPE : 
    case ProjectTreeNode.INPUTTYPE : 
    case ProjectTreeNode.OUTPUTTYPE : */
		default: //mai pe scurt:
			ProjectTreeNode theNode = (ProjectTreeNode) ((DefaultMutableTreeNode)
					path.getPathComponent(path.getPathCount() - 1)).getUserObject();
			project.jumpToLocation(theNode.fileName, node.line);
			break;
		}
	}
}
